<!DOCTYPE html>
<html>

<head>

    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">


    <title>ZDH 用户中心</title>
    <meta name="keywords" content="ZDH 用户中心">
    <meta name="description" content="ZDH 用户中心">

    <link rel="shortcut icon" href="img/favicon.ico">
    <link href="css/plugins/chosen/chosen.css" rel="stylesheet">
    <link href="css/bootstrap.min.css?v=3.3.6" rel="stylesheet">
    <link href="css/font-awesome.css?v=4.4.0" rel="stylesheet">
    <link href="css/animate.css" rel="stylesheet">
    <link href="css/style.css?v=4.1.0" rel="stylesheet">
    <link href="css/plugins/switchery/switchery.css" rel="stylesheet">


</head>

<body class="gray-bg">
    <div class="wrapper wrapper-content animated fadeInRight">

        <div class="row">

            <div class="col-sm-12">
                <div class="ibox float-e-margins">
                    <div class="ibox-title">
                        <h5>用户中心</h5>
                        <div class="ibox-tools">
                            <a class="collapse-link">
                                <i class="fa fa-chevron-up"></i>
                            </a>
                            <a class="dropdown-toggle" data-toggle="dropdown" href="buttons.html#">
                                <i class="fa fa-eye"></i>
                            </a>
                            <ul class="dropdown-menu dropdown-user">
                                <li><a href="javascript:void(0);" onclick="getResourceDesc()">功能说明</a>
                                </li>
                            </ul>
                            <a class="close-link">
                                <i class="fa fa-times"></i>
                            </a>
                        </div>
                    </div>
                    <div class="ibox-content">
                        <form class="form-horizontal m-t" id="signupForm">

                            <div class="form-group">
                                <label class="col-sm-3 control-label">用户账号：</label>
                                <div class="col-sm-8">
                                    <input id="user_account" name="user_account" class="form-control" type="text" aria-required="true" aria-invalid="true" class="error">
                                </div>
                            </div>

                            <div class="form-group">
                                <label class="col-sm-3 control-label">用户名：</label>
                                <div class="col-sm-8">
                                    <input id="user_name" name="user_name" class="form-control" type="text" aria-required="true" aria-invalid="true" class="error">
                                </div>
                            </div>
                            <div class="form-group">
                                <label class="col-sm-3 control-label">密码：</label>
                                <div class="col-sm-8">
                                    <input id="user_password" name="user_password" class="form-control" type="password">
                                </div>
                            </div>
                            <div class="form-group">
                                <label class="col-sm-3 control-label">确认密码：</label>
                                <div class="col-sm-8">
                                    <input id="confirm_password" name="confirm_password" class="form-control" type="password">
                                    <span class="help-block m-b-none"><i class="fa fa-info-circle"></i> 请再次输入您的密码</span>
                                </div>
                            </div>
                            <div class="form-group">
                                <label class="col-sm-3 control-label">E-mail：</label>
                                <div class="col-sm-8">
                                    <input id="email" name="email" class="form-control" type="email">
                                </div>
                            </div>
                            <div class="form-group">
                                <label class="col-sm-3 control-label">开启邮件监控：</label>
                                <div class="col-sm-8 switch">
                                    <div class="onoffswitch">
                                        <input type="checkbox" checked class="onoffswitch-checkbox" id="is_use_email" name="is_use_email">
                                        <label class="onoffswitch-label" for="is_use_email">
                                            <span class="onoffswitch-inner"></span>
                                            <span class="onoffswitch-switch"></span>
                                        </label>
                                    </div>
                                </div>
                            </div>
                            <div class="form-group">
                                <label class="col-sm-3 control-label">手机号：</label>
                                <div class="col-sm-8">
                                    <input id="phone" name="phone" class="form-control" type="text" aria-required="true" aria-invalid="true" class="error">
                                </div>
                            </div>
                            <div class="form-group">
                                <label class="col-sm-3 control-label">开启手机监控：</label>
                                <div class="col-sm-8 switch">
                                    <div class="onoffswitch">
                                        <input type="checkbox" checked class="onoffswitch-checkbox" id="is_use_phone" name="is_use_phone">
                                        <label class="onoffswitch-label" for="is_use_phone">
                                            <span class="onoffswitch-inner"></span>
                                            <span class="onoffswitch-switch"></span>
                                        </label>
                                    </div>
                                </div>
                            </div>
                            <div class="form-group">
                                <label class="col-sm-3 control-label">签名</label>
                                <div class="col-sm-8">
                                    <input id="signature" name="signature" class="form-control" type="text" aria-required="true" aria-invalid="true" class="error">
                                </div>
                            </div>

                            <div class="form-group">
                                <label class="col-sm-3 control-label">产品</label>
                                <div class="col-sm-8">
                                    <select id="product_code" name="product_code"
                                            data-placeholder="产品code...."
                                            class="chosen-select form-control m-b" >
                                    </select>
                                </div>
                            </div>

                            <div class="form-group">
                                <label class="col-sm-3 control-label">用户组</label>
                                <div class="col-sm-8">
                                    <select id="user_group" name="user_group"
                                            data-placeholder=""
                                            class="chosen-select form-control m-b" tabindex="2">
                                        <option value="" mytype="">空</option>
                                    </select>
                                </div>
                            </div>

                            <div class="form-group">
                                <label class="col-sm-3 control-label">归属上级</label>
                                <div class="col-sm-8">
                                    <select id="user_team" name="user_team"
                                            data-placeholder=""
                                            class="chosen-select form-control m-b" tabindex="2">
                                        <option value="" mytype="">空</option>
                                    </select>
                                </div>
                            </div>

                            <div class="form-group">
                                <label class="col-sm-3 control-label">角色</label>
                                <div class="col-sm-8">
                                    <select id="roles" name="roles"
                                            data-placeholder="空" multiple
                                            class="chosen-select form-control m-b" tabindex="2">
                                        <option value="" mytype="">空</option>
                                    </select>
                                </div>
                            </div>

                            <div class="form-group">
                                <label class="col-sm-3 control-label">数据标识组</label>
                                <div class="col-sm-8">
                                    <select id="tag_group_code" name="tag_group_code"
                                            data-placeholder=""
                                            class="chosen-select form-control m-b" tabindex="2">
                                        <option value="" mytype="">空</option>
                                    </select>
                                </div>
                            </div>

                            <div class="form-group">
                                <div class="col-sm-8 col-sm-offset-3">
                                    <button class="btn btn-primary" type="button" id="btn_up" οnsubmit='return false'>更新</button>
                                </div>
                            </div>
                        </form>
                    </div>
                </div>
            </div>
        </div>

    </div>

    <script src="js/zdh_common.js"></script>
    <!-- 全局js -->
    <script src="js/jquery.min.js?v=2.1.4"></script>
    <script src="js/bootstrap.min.js?v=3.3.6"></script>

    <!-- 自定义js -->
    <script src="js/content.js?v=1.0.0"></script>

    <!-- jQuery Validation plugin javascript-->
    <script src="js/plugins/validate/jquery.validate.min.js"></script>
    <script src="js/plugins/validate/messages_zh.min.js"></script>

    <!-- Switchery -->
    <script src="js/plugins/switchery/switchery.js"></script>

    <!-- layer javascript -->
    <script src="js/plugins/layer/layer.min.js"></script>

    <script src="js/plugins/chosen/chosen.jquery.js"></script>

    <script>

        (function(document, window, $) {


            var url = location.search; //这一条语句获取了包括问号开始到参数的最后，不包括前面的路径
            var params = url.substr(1);//去掉问号
            var pa = params.split("&");
            var s = new Object();//定义一个全局变量-存储任务ID
            for (var i = 0; i < pa.length; i++) {
                s[pa[i].split("=")[0]] = unescape(pa[i].split("=")[1]);
            }

            id = s['id'];

            $('#user_group').chosen();
            $('#roles').chosen();
            $('#tag_group_code').chosen();
            $('#user_team').chosen();

            $.ajax({
                type: 'POST',
                url: server_context+"/product_tag_list",
                async:false,
                dataType: 'json',
                data: "",
                //成功返回
                success: function (data) {
                    if(data.code != "200"){
                        layer.msg(data.msg);
                        return ;
                    }
                    var str = '';
                    for (var i = 0; i < data.result.length; i++) {
                        str += '<option value=\"' + data.result[i].product_code + '\" hassubinfo=\"true\">' + data.result[i].product_name + '</option>';
                    }
                    $('#product_code').html(str);
                    $("#product_code").trigger("chosen:updated");
                    $('#product_code').chosen().on("change", function (evt, params) {
                        //更新用户组和角色
                        init_role($("#product_code").val());
                        init_user_group($("#product_code").val());
                        init_data_tag($("#product_code").val());
                        init_user_leader($("#product_code").val());
                        //init_jstree($("#product_code").val());
                    });
                    init_role($("#product_code").val());
                    init_user_group($("#product_code").val());
                    init_data_tag($("#product_code").val());
                    init_user_leader($("#product_code").val());
                },
                //处理完成
                complete: function () {
                },
                //报错
                error: function (data) {
                }
            });

            function init_role(product_code){
                $.ajax({
                    type: 'POST',
                    url: server_context+"/role_list?enable=true&product_code="+product_code,
                    async:false,
                    dataType: 'json',
                    data: '',
                    //成功返回
                    success: function (data) {
                        if(data.code != "200"){
                            layer.msg(data.msg);
                            return ;
                        }
                        var str = '<option value=\"\" hassubinfo=\"true\"></option>';
                        for (var i = 0; i < data.result.length; i++) {
                            str += '<option value=\"' + data.result[i].code + '\" hassubinfo=\"true\">' + data.result[i].name + '</option>'
                        }
                        $('#roles').html(str);
                        $("#roles").trigger("chosen:updated");
                        $('#roles').chosen();
                    },
                    //处理完成
                    complete: function () {
                        console.info("complete getUserInfo")
                    },
                    //报错
                    error: function (data) {
                        console.info("error: " + data.responseText);
                    }
                });
            }

            function init_user_group(product_code){
                $.ajax({
                    type: 'POST',
                    url: server_context+"/user_group_list?enable=true&product_code="+product_code,
                    async:false,
                    dataType: 'json',
                    data: '',
                    //成功返回
                    success: function (data) {

                        var str = '<option value=\"\" hassubinfo=\"true\"></option>';
                        for (var i = 0; i < data.result.length; i++) {
                            str += '<option value=\"' + data.result[i].group_code + '\" hassubinfo=\"true\">' + data.result[i].name + '</option>'
                        }
                        $('#user_group').html(str);
                        $("#user_group").trigger("chosen:updated");
                        $('#user_group').chosen();
                    },
                    //处理完成
                    complete: function () {
                        console.info("complete getUserInfo")
                    },
                    //报错
                    error: function (data) {
                        console.info("error: " + data.responseText);
                    }
                });
            }

            function init_data_tag(product_code){
                $.ajax({
                    type: 'post',
                    url: server_context+"/data_tag_group_list?product_code="+product_code,
                    async:false,
                    dataType: 'json',
                    data: '',
                    //成功返回
                    success: function (data) {

                        if(data.code != "200"){
                            layer.msg(data.msg);
                            return ;
                        }

                        var str = '<option value=\"\" hassubinfo=\"true\"></option>';
                        for (var i = 0; i < data.result.length; i++) {
                            str += '<option value=\"' + data.result[i].tag_group_code + '\" hassubinfo=\"true\">' + data.result[i].tag_group_name + '</option>'
                        }
                        $('#tag_group_code').html(str);
                        $("#tag_group_code").trigger("chosen:updated");
                        $('#tag_group_code').chosen();
                    },
                    //处理完成
                    complete: function () {
                        console.info("complete data_tag_group_list")
                    },
                    //报错
                    error: function (data) {
                        console.info("error: " + data.responseText);
                    }
                });
            }

            function init_user_leader(product_code){
                $.ajax({
                    type: 'post',
                    url: server_context+"/user_account_list?product_code="+product_code,
                    async:false,
                    dataType: 'json',
                    data: '',
                    //成功返回
                    success: function (data) {

                        if(data.code != "200"){
                            layer.msg(data.msg);
                            return ;
                        }

                        var str = '<option value=\"\" hassubinfo=\"true\"></option>';
                        for (var i = 0; i < data.result.length; i++) {
                            str += '<option value=\"' + data.result[i] + '\" hassubinfo=\"true\">' + data.result[i] + '</option>'
                        }
                        $('#user_team').html(str);
                        $("#user_team").trigger("chosen:updated");
                        $('#user_team').chosen();
                    },
                    //处理完成
                    complete: function () {
                        console.info("complete data_tag_group_list")
                    },
                    //报错
                    error: function (data) {
                        console.info("error: " + data.responseText);
                    }
                });
            }

            if(id != '-1'){
                $.ajax({
                    type: 'POST',
                    url: server_context+"/user_detail",
                    async:false,
                    dataType: 'json',
                    data: 'id='+id,
                    //成功返回
                    success: function (data) {

                        if(data.code != "200"){
                            layer.msg("查询失败");
                            return ;
                        }
                        if(data.result.user_name !='' ){
                            $('#user_account').val(data.result.user_account);
                            $('#user_name').val(data.result.user_name);
                            $('#email').val(data.result.email);
                            $('#signature').val(data.result.signature);
                            $('#user_password').val("");
                            if(data.result.is_use_email != "on"){
                                $('#is_use_email').prop("checked",false)
                                //$('#is_use_email').find("input[type='checkbox']").onoff();
                            }
                            $('#phone').val(data.result.phone);
                            if(data.result.is_use_phone != "on"){
                                $('#is_use_phone').prop("checked",false)
                                //$('#is_use_email').find("input[type='checkbox']").onoff();
                            }

                            if(data.result.product_code != ""){
                                $("#product_code").val(data.result.product_code);
                                $("#product_code").trigger("chosen:updated");
                            }

                            if(data.result.user_group != ""){
                                $("#user_group").val(data.result.user_group);
                                $("#user_group").trigger("chosen:updated");
                            }

                            if(data.result.roles != ""){
                                $("#roles").val(data.result.roles.split(","));
                                $("#roles").trigger("chosen:updated");
                            }

                            if(data.result.tag_group_code != ""){
                                $("#tag_group_code").val(data.result.tag_group_code.split(","));
                                $("#tag_group_code").trigger("chosen:updated");
                            }

                        }
                    },
                    //处理完成
                    complete: function () {
                        console.info("complete getUserInfo")
                    },
                    //报错
                    error: function (data) {
                        console.info("error: " + data.responseText);
                    }
                });
            }else{
                //新增

            }


            $('#btn_up').click(function () {

                if($('#user_password').val() != $('#confirm_password').val()){
                    layer.msg("2次密码不一致,请检查密码");
                    return ;
                }

                $.ajax({
                    type: 'POST',
                    url: server_context+"/user_update",
                    dataType: 'json',
                    data: $("#signupForm").serialize()+"&id="+id,
                    //发送数据前
                    beforeSend: function () {
                        // 禁用按钮防止重复提交
                        $("#btn_up").attr({disabled: "disabled"});
                    },
                    //成功返回
                    success: function (data) {
                        $("#btn_up").removeAttr('disabled');
                        if(data.code == "200"){
                            layer.msg('更新成功', {
                                time: 2000, //2s后自动关闭
                                btn: ['明白了,5s后自动关闭']
                            },function () {
                                $(".J_menuTab.active",window.top.document).find(".fa.fa-times-circle").click();
                            });
                        }else{
                            layer.msg('更新失败,'+data.result, {
                                time: 2000, //20s后自动关闭
                                btn: ['明白了,2s后自动关闭']
                            },function () {
                                // $(".J_menuTab.active",window.top.document).find(".fa.fa-times-circle").click();
                            });
                        }

                    },
                    //处理完成
                    complete: function () {
                        $("#btn_up").removeAttr('disabled');
                        console.info("complete")
                    },
                    //报错
                    error: function (data) {
                        $("#btn_up").removeAttr('disabled');
                        layer.msg(data.responseText);
                        console.info("error: " + data.responseText);
                    }
                });


            })

        })(document, window, jQuery);


    </script>

</body>



</html>
